﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mozlite.Common;

namespace Mozlite.Web.UI.Areas.ControlPanel.Controllers
{
    public class UCenterController : ControlPanelBaseController
    {
        public ActionResult UCenterView()
        {
            return View(UCenterApplication.Current.Configuration);
        }

        [HttpPost]
        public ActionResult UCenterView(UCenterConfiguration model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                model.ApplicationID = UCenterApplication.Current.ApplicationID;
                model.DefaultRoleIDs = form["DefaultRoleIDs"];
                if (Applications.SaveConfiguration<UCenterConfiguration>(model))
                    SetResourceStatus("ConfigurationBase.Save.Success");
                else
                    SetResourceStatus("ConfigurationBase.Save.Error", true);
            }
            return View(UCenterApplication.Current.Configuration);
        }

        public ActionResult RolesView()
        {
            return View(Roles.LoadRoles().Values);
        }

        public ActionResult PermissionView()
        {
            return View(Permissions.LoadPermissions<UCenterPermission>());
        }

        [HttpPost]
        public ActionResult PermissionView(FormCollection form)
        {
            var permissions = new List<UCenterPermission>();
            foreach (var roleID in Roles.LoadRoles().Keys) {
                permissions.Add(UCenterPermission.GetForm<UCenterPermission>(roleID, form));
            }
            if (Permissions.SavePermissions<UCenterPermission>(permissions))
                SetResourceStatus("Permission.Save.Success");
            else
                SetResourceStatus("Permission.Save.Error");
            return View(Permissions.LoadPermissions<UCenterPermission>());
        }
    }
}
